package cn.edu.cug.cs.gtl.gsm.jdbc.oracle;

import cn.edu.cug.cs.gtl.gsm.item.MetaItem;
import cn.edu.cug.cs.gtl.util.StringUtils;

import java.io.IOException;
import java.sql.*;

public class MetaTable {
    private String META_TABLE_NAME;
    private String SQL_DROP_META_TABLE;
    private String SQL_CREATE_META_TABLE;
    private String SQL_DELETE_META_TABLE_DATA;
    private String SQL_SELECT_META_TABLE_DATA;
    private String SQL_DELETE_ALL_META_TABLE_DATA;
    private String SQL_INSERT_META_TABLE_DATA;
    private String SQL_UPDATE_META_TABLE_DATA;
    private String SQL_SELECT_FOR_UPDATE_META_TABLE_DATA;

    protected Connection connection;


    public MetaTable(Connection connection, String baseName) {
        this.connection = connection;
        META_TABLE_NAME=baseName+".rdb_sm_meta";
        SQL_DROP_META_TABLE= "DROP TABLE "+META_TABLE_NAME;
        SQL_CREATE_META_TABLE= "CREATE TABLE " + META_TABLE_NAME + " ( base_name  VARCHAR2(255) PRIMARY KEY, meta CLOB)";
        SQL_DELETE_META_TABLE_DATA= "DELETE FROM " + META_TABLE_NAME + " WHERE base_name = ?";
        SQL_SELECT_META_TABLE_DATA= "SELECT meta FROM " + META_TABLE_NAME + " WHERE base_name = ?";
        SQL_DELETE_ALL_META_TABLE_DATA= "DELETE FROM " + META_TABLE_NAME ;
        SQL_INSERT_META_TABLE_DATA= "INSERT INTO " + META_TABLE_NAME + " VALUES (?,?)";
        SQL_UPDATE_META_TABLE_DATA= "UPDATE " + META_TABLE_NAME + " SET meta = empty_clob() WHERE base_name=? ";
        SQL_SELECT_FOR_UPDATE_META_TABLE_DATA= "SELECT meta FROM " + META_TABLE_NAME + " WHERE base_name = ? FOR UPDATE";
    }


    public boolean create() throws SQLException {
        Statement statement = connection.createStatement();
        boolean r =  statement.execute(SQL_CREATE_META_TABLE);
        statement.close();
        return r;
    }

    public boolean drop() throws SQLException {
        if(exists()) {
            Statement statement = connection.createStatement();
            boolean r = statement.execute(SQL_DROP_META_TABLE);
            statement.close();
            return r;
        }
        else
            return true;
    }

    public boolean delete(String baseName) throws SQLException{
        PreparedStatement statement = connection.prepareStatement(SQL_DELETE_META_TABLE_DATA);
        statement.setString(1,baseName);
        boolean r =  statement.execute();
        statement.close();
        return r;
    }

    public boolean deleteAll() throws SQLException{
        Statement statement = connection.createStatement();
        boolean r =  statement.execute(SQL_DELETE_ALL_META_TABLE_DATA);
        statement.close();
        return r;
    }

    public boolean insert(String baseName, MetaItem metaItem) throws SQLException, IOException {

        PreparedStatement statement = connection.prepareStatement(SQL_INSERT_META_TABLE_DATA);
        statement.setString(1,baseName);
        Clob b = connection.createClob();
        b.setString(1,metaItem.storeToCompressString());
        statement.setClob(2,b);
        statement.execute();
        statement.close();
        return true;
    }

    public boolean update(String baseName, MetaItem metaItem) throws SQLException, IOException {
        //设置为空CLOB
        PreparedStatement statement = connection.prepareStatement(SQL_UPDATE_META_TABLE_DATA);
        statement.setString(1,baseName);
        statement.executeUpdate();
        statement.close();
        //更新内容
        statement = connection.prepareStatement(SQL_SELECT_FOR_UPDATE_META_TABLE_DATA);
        statement.setString(1,baseName);
        ResultSet rs = statement.executeQuery();
        if(rs.next()) {
            Clob b = rs.getClob(1);
            b.setString(1,metaItem.storeToCompressString());
            statement.executeUpdate();
        }
        rs.close();
        statement.close();
        return true;
    }

    public MetaItem select(String baseName) throws SQLException, IOException {
        PreparedStatement statement = connection.prepareStatement(SQL_SELECT_META_TABLE_DATA);
        statement.setString(1,baseName);
        ResultSet r =  statement.executeQuery();
        Clob compressZip = null;
        while(r.next()){
            compressZip = r.getClob(1);
            long l = compressZip.length();
            if(compressZip!=null) {
                MetaItem metaItem = new MetaItem(0, 0);
                metaItem.loadFromCompressString(compressZip.getSubString(1,(int)l));
                r.close();
                statement.close();
                return metaItem;
            }
            else {
                r.close();
                statement.close();
                return null;
            }
        }
        r.close();
        statement.close();
        return null;
    }

    public boolean exists() throws SQLException{
        String []sa = StringUtils.split(META_TABLE_NAME,".");
        return OracleStorageManager.exists(connection,sa[0],sa[1]);
    }
}
